function compareTime(startTime, endTime) {
    // var thisResult = Date.parse(endTime.replace(/-/g, "/")) - Date.parse(startTime.replace(/-/g, "/"));
    var thisResult = new Date(returnStard(endTime)) - new Date(returnStard(startTime));
    if (thisResult < 0) {
        return true;
    } else if (thisResult > 0) {
        return false;
    } else if (thisResult == 0) {
        return true;
    } else {
        return false;
    }
}

function DateMinus (sDate, eDate){
    var sdate = new Date(returnStard(sDate));
    var now = new Date(returnStard(eDate));
    var milliseconds = now.getTime() - sdate.getTime();  //毫秒
    var minus = parseInt(milliseconds / (1000 * 60 ));  //分
    return minus;
}

function DateHours (sDate, eDate){
    var sdate = new Date(returnStard(sDate));
    var now = new Date(returnStard(eDate));
    var milliseconds = now.getTime() - sdate.getTime();  //毫秒
    var hours = Math.ceil(milliseconds / (1000 * 60 * 60));  //时
    return hours;
}

function DateDays(sDate, eDate) {
    var sdate = new Date(returnStard(sDate));
    var now = new Date(returnStard(eDate));
    var days = now.getTime() - sdate.getTime();
    var day = parseInt(days / (1000 * 60 * 60*24));
    return day;
}

function getNowFormatDate() {
    return new Date().Format('yyyy-MM-dd hh:mm:ss');  //依赖 lib下的prototype
}

function addDate(date, days) {
    return addDay(date, days).Format("yyyy-MM-dd hh:mm:ss");
}

function addHour(date, hours) {
    if (hours == undefined || hours == '') {
        hours = 1;
    }
    var oneHour = 60*60*1000;
    var date = new Date(new Date(date).getTime() + oneHour * hours).getTime();
    return date;
}

function addDay(date, days) {
    if (days == undefined || days == '') {
        days = 1;
    }
    var oneDay = 1000*60*60*24;
    var date = new Date(new Date(date).getTime() + oneDay * days);
    return date;
}

function addMonth(date,addMonthNum){
    var date = new Date(date);
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    month += addMonthNum;
    if( (month/12) > 1) {
        year += Math.floor(month/12);
        month = month%12;
    }
    // console.log(year,month,day,hour,minutes);
    var returnDate = new Date(year+'/'+month+'/'+day+' '+hour+':' + minutes +':' + seconds);
    return returnDate;
}



function returntime(countTime,time){   //返回一个对象，对象里时分秒   time为1，毫秒为单位，  为1000，秒为单位
	
    var day =  Math.floor(countTime / time / 60 / 60 / 24);
    
    
    var hour = Math.floor(countTime / time / 60 / 60)%24; 
    
    var minute = Math.floor(countTime / time / 60)%60; 
    
    var second = Math.floor(countTime / time)%60;
    day = day < 10 ? '0' + day : ''+day;
    hour = hour < 10 ? '0' + hour :''+hour;
    minute = minute < 10 ? '0' + minute :''+minute;
    second = second < 10 ? '0' + second :''+second;
    return {
        day,
        hour,
        minute,
        second
    }
}

var setInter = null;
function startCountDownTime(obj,endTime,startTime) {  //开启倒计时  结束时间 开始时间，
    // endTime = 1517414400000;  // 2018/02/01
    if(endTime <= startTime) {
        return;
    }
    if(!startTime) {
        startTime = new Date().getTime();
    }
    startCountDownTime2(obj, endTime - startTime, 1000);  //毫秒单位
}

function startCountDownTime2(obj, countTime, time) {      //开启倒计时，直接给予比较后的时间  time为1000则是毫秒单位  为1则是秒单位
    var obj2;
    obj2 = returntime(countTime, time);
    obj.day = obj2.day;
    obj.hour = obj2.hour;
    obj.minute = obj2.minute;
    obj.second = obj2.second;
    countTime = countTime - time;  // time只能为1和1000   分别对应秒和毫秒两个单位

    setInter = setInterval(function() {
        obj2 = returntime(countTime, time);
        obj.day = obj2.day;
        obj.hour = obj2.hour;
        obj.minute = obj2.minute;
        obj.second = obj2.second;
        countTime = countTime - time;  // time只能为1和1000   分别对应秒和毫秒两个单位
    }, 1000);
}

function stopCountDownTime() {  //关闭倒计时
    clearInterval(setInter);
    setInter = null;
}

function returnStard(timeStr){
    return timeStr.replace(/-/g, '/').replace(/T/g, ' ').substring(0,19);
}

export default {
    compareTime,
    DateMinus,
    DateHours,
    DateDays,
    getNowFormatDate,
    addDay,
    addHour,
    addDate,
    startCountDownTime,
    startCountDownTime2,
    stopCountDownTime,
    addMonth,
    returnStard
}